package com.axter.chrome;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

import java.io.*;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * chrome bookmark html template
 * Created by zhaobo on 2016/11/18.
 */
public class TemplateUtils {
    public static void main(String[] args) throws IOException, TemplateException {

    }

    private static final String header = "<!DOCTYPE NETSCAPE-Bookmark-file-1>\n" +
            "<!-- This is an automatically generated file.\n" +
            "     It will be read and overwritten.\n" +
            "     DO NOT EDIT! -->\n" +
            "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">\n" +
            "<TITLE>Bookmarks</TITLE>\n" +
            "<H1>Bookmarks</H1>\n" +
            "<DL><p>\n";
    private static final String footer = "</DL><p>";

    public static void getBootmarks2(String html) throws IOException {
        FileOutputStream fos = new FileOutputStream(new File("bookmarks_" + System.currentTimeMillis() + ".html"));
        fos.write(header.getBytes("utf8"));
        fos.write(html.getBytes("utf8"));
        fos.write(footer.getBytes("utf8"));
        fos.flush();
        fos.close();
    }

    public static void getBootmarks(String html) throws IOException, TemplateException {


        // Create your Configuration instance, and specify if up to what FreeMarker
        // version (here 2.3.25) do you want to apply the fixes that are not 100%
        // backward-compatible. See the Configuration JavaDoc for details.
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);

        // Specify the source where the template files come from. Here I set a
        // plain directory for it, but non-file-system sources are possible too:
        //        File dir = new File("");
        //        if (!dir.exists()) {
        //            if (!dir.mkdirs()) {
        //                throw new RuntimeException("模板目录创建失败");
        //            }
        //        }
        cfg.setClassForTemplateLoading(html.getClass(), "/");

        // Set the preferred charset template files are stored in. UTF-8 is
        // a good choice in most applications:
        cfg.setDefaultEncoding("UTF-8");
        cfg.setLocale(Locale.CHINESE);
        // Sets how errors will appear.
        // During web page *development* TemplateExceptionHandler.HTML_DEBUG_HANDLER is better.
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

        // Don't log exceptions inside FreeMarker that it will thrown at you anyway:
        cfg.setLogTemplateExceptions(false);

        Template temp = cfg.getTemplate("bookmarks.html", "UTF-8");
        temp.setLocale(Locale.CHINESE);
        // Create the root hash. We use a Map here, but it could be a JavaBean too.
        Map<String, String> root = new HashMap<>();

        // Put string "user" into the root
        root.put("html", html);

        FileOutputStream baos = new FileOutputStream("bookmarks_" + System.currentTimeMillis() + ".html");
        Writer out = new OutputStreamWriter(baos);
        temp.process(root, out);
    }
}
